package 代码记录.指定公司代码练习记录.华为_2024.分值100;

import java.util.*;

/**
 * @author zx
 * @create 2024-01-31 12:59
 * 自己没做出来，二刷
 */
public class 字符串摘要_11 {
    /**
     * 准备改装人家的思路，前面改对了，但是后面没看懂
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i < str.length();i++){
            if(Character.isLetter(str.charAt(i))){
                sb.append(Character.toLowerCase(str.charAt(i)));
            }
        }
        int count = 1;//初始化为1，用来记录相同字符的出现次数。
        char currentChar = sb.charAt(sb.length() - 1);
        List<Map.Entry<Character, Integer>> charCountList = new ArrayList<>();
        Map<Character, Integer> charCountMap = new HashMap<>();
        //通过循环遍历filteredStr，从倒数第二个字符开始取出字符并进行比较
        for(int i = sb.length() - 2;i >= 0;i--){
            char ch = sb.charAt(i);
            //如果相邻字符相同，则计数器count加1
            if(currentChar == ch){
                count++;
            }else{
                //如果相邻字符不同，则将当前字符的出现次数和字符添加到charCountMap中，并将键值对添加到charCountList中
                if(count == 1){
                    count += charCountMap.getOrDefault(currentChar, 0) - 1;
                    charCountMap.put(currentChar, count + 1);
                } else {
                    charCountMap.put(currentChar, count);
                }
                charCountList.add(new AbstractMap.SimpleEntry<>(currentChar, count));
                currentChar = ch;
                count = 1;
            }
            if (i == 0) {
                if (count == 1) {
                    count += charCountMap.getOrDefault(currentChar, 0) - 1;
                }
                charCountList.add(new AbstractMap.SimpleEntry<>(currentChar, count));
            }
        }
        //按照出现次数降序和字母顺序对charCountList进行排序
        charCountList.sort((a, b) -> {
            if (a.getValue().equals(b.getValue())) {
                return a.getKey() - b.getKey();
            }
            return b.getValue() - a.getValue();
        });

        StringBuilder result = new StringBuilder();
        for (Map.Entry<Character, Integer> entry : charCountList) {
            result.append(entry.getKey()).append(entry.getValue());
        }
        System.out.println(result.toString());
    }
}
